package com.anhuanjia.tools.eao.controller;

import java.util.Date;

import com.anhuanjia.tools.eao.bean.EaoCompanyInfo;
import com.efastsoft.framework.Member;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;

/**
 * 机构资质外键表
 * 
 * @author Administrator
 *
 */
@Alias(value = "companyQualifications")
public class CompanyQualificationsUI extends UIFacade {

	@Priv(loginType = LoginType.Member)
	public void saveOrUdate() {
		DataTable ecMembe = new Q(" select em.Type,ep.Id from ec_member em left join eao_is_engineer eie on eie.Member = em.ID left join eao_Position ep on eie.IsEngineer = ep.Id where em.Id = ?", Member.getUID()).fetch();
		if(ecMembe.getRowCount()>0){
			Integer position = 0;
			if(ecMembe.get(0).getInt("Id")<8){
				position = ecMembe.get(0).getInt("Id");
			}else{
				position = ecMembe.get(0).getInt("ParentId");
			}
			if(!"Ent".equals(ecMembe.get(0).getString("Type")) && position !=1 && position!=2){//总工总经理一级账号有权限
				f("您没有权限更改机构缩写名称和统一社会信用代码");
				return;
			}
		}
		DataTable dataTable = new Q(" select * from eao_company_info where Id = ?", Member.getCompany())
				.fetch();
		Date date = new Date();
		if (dataTable.getRowCount() != 0) {
			EaoCompanyInfo qualifications = new EaoCompanyInfo();
			qualifications.setValue(dataTable.get(0));
			qualifications.setValue(this.Request);
			qualifications.setModifyTime(date);
			qualifications.setModifyUser(Member.getUID());
			if(qualifications.update()) {
				s("机构信息修改成功");
			} else {
				f("机构信息修改失败");
			}
		} else {
			EaoCompanyInfo qualifications = new EaoCompanyInfo();
			qualifications.setValue(this.Request);
			qualifications.setId(Member.getCompany());
			qualifications.setAddTime(date);
			qualifications.setAddUser(Member.getUID());
			qualifications.setModifyTime(date);
			qualifications.setModifyUser(Member.getUID());
			if(qualifications.insert()) {
				s("机构信息修改成功");
			} else {
				f("机构信息修改失败");
			}
		}
	}
	
	@Priv(loginType = LoginType.Member)
	public void findOne() {
		DataTable dataTable = new Q(" select * from eao_company_info where Id = ?", Member.getCompany()).fetch();
		if (dataTable.getRowCount() != 0) {
			EaoCompanyInfo qualifications = new EaoCompanyInfo();
			qualifications.setValue(dataTable.get(0));
			this.Response.put("qualifications", qualifications);
		}
	}
}
